<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addApprovalSealTaskForm" ref="addApprovalSealTaskRef" :rules="addApprovalSealTaskrules">
                        <el-form-item label="印章表的外键" :label-width="formLabelWidth"
                                      prop="sealId" v-if="addApprovalSealTaskForm.id">
                            <el-input
                                    v-model="addApprovalSealTaskForm.sealId"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写印章表的外键"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="申请人姓名" :label-width="formLabelWidth"
                                      prop="applicant">
                            <el-input
                                    v-model="addApprovalSealTaskForm.applicant"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写申请人姓名"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="申请人提交时间" :label-width="formLabelWidth"
                                      prop="applicantAt" v-if="addApprovalSealTaskForm.id">
                            <el-input
                                    v-model="addApprovalSealTaskForm.applicantAt"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写申请人提交时间"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="文件名称" :label-width="formLabelWidth"
                                      prop="files">
                            <el-input
                                    v-model="addApprovalSealTaskForm.files"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写文件名称"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="是否为最后一个审批级别" :label-width="formLabelWidth"
                                      prop="isLast" v-if="addApprovalSealTaskForm.id">
                            <el-input
                                    v-model="addApprovalSealTaskForm.isLast"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写是否为最后一个审批级别"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="审批级别" :label-width="formLabelWidth"
                                      prop="level" v-if="addApprovalSealTaskForm.id">
                            <el-input
                                    v-model="addApprovalSealTaskForm.level"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写审批级别"
                            ></el-input>
                        </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script>
    import {ref} from "vue";
    import {httpService} from "@/utils/httpService";
    import {ElMessage, ElForm} from "element-plus";
    import {vxRule} from "@/utils/validator";

    export default {
        name: "AddApprovalSealTask",
        components: {ElForm, ElMessage},
        data() {
            return {
                dialogFormVisible: ref(false),
                formLabelWidth: "120px",
                title: "",
                personnelType: "",
                addApprovalSealTaskForm: {
                    sealId: "",
                    applicant: "",
                    applicantAt: "",
                    files: "",
                    isLast: "",
                    level: "",
                },
                // 校验规则
                addApprovalSealTaskrules: {
                    applicant: vxRule(true, null, "blur"),
                    files: vxRule(true, null, "blur"),
                },
            };
        },
        methods: {
            closeDialog() {
                this.clear();
            },
            cancel() {
                this.dialogFormVisible = false;
                this.clear();
            },
            getApprovalSealTaskById(id) {
                httpService("/javaApi/approvalSealTask/queryById", {id: id}, "get").then(
                    (data) => {
                        this.addApprovalSealTaskForm = data;
                    }
                );
            },
            clear() {
                this.dialogFormVisible = false;
                //清空form
                    this.addApprovalSealTaskForm.id = "";
                    this.addApprovalSealTaskForm.sealId = "";
                    this.addApprovalSealTaskForm.applicant = "";
                    this.addApprovalSealTaskForm.applicantAt = "";
                    this.addApprovalSealTaskForm.files = "";
                    this.addApprovalSealTaskForm.isLast = "";
                    this.addApprovalSealTaskForm.level = "";
            },
            save() {
                this.$refs.addApprovalSealTaskRef.validate((valid) => {
                    if (valid) {
                        let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                        params.append("sealId", this.addApprovalSealTaskForm.sealId);
                        params.append("applicant", this.addApprovalSealTaskForm.applicant);
                        params.append("applicantAt", this.addApprovalSealTaskForm.applicantAt);
                        params.append("files", this.addApprovalSealTaskForm.files);
                        params.append("isLast", this.addApprovalSealTaskForm.isLast);
                        params.append("level", this.addApprovalSealTaskForm.level);
                        if (this.addApprovalSealTaskForm.id) {
                            params.append("id", this.addApprovalSealTaskForm.id);
                            httpService("/javaApi/approvalSealTask/edit", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("修改成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getApprovalSealTaskList();
                                    } else {
                                        ElMessage.error("修改失败");
                                    }
                                }
                            );
                        } else {
                            httpService("/javaApi/approvalSealTask/add", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("添加成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getApprovalSealTaskList();
                                    } else {
                                        ElMessage.error("添加失败");
                                    }
                                }
                            );
                        }
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>
<style>
</style>